package com.srm.psu.glf;

import javax.microedition.khronos.opengles.GL10;

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.Scene.ITouchArea;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.entity.util.FPSLogger;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.font.FontFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlasTextureRegionFactory;
import org.anddev.andengine.entity.scene.Scene.IOnAreaTouchListener;
import org.anddev.andengine.input.touch.TouchEvent;
import org.anddev.andengine.opengl.texture.region.TextureRegion;
import org.anddev.andengine.util.HorizontalAlign;
import org.anddev.andengine.ui.activity.BaseGameActivity;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;

public class ScoresActivity extends BaseGameActivity implements IOnAreaTouchListener 
{
        // ===========================================================
        // Constants
        // ===========================================================

		private static final int CAMERA_WIDTH = 480;
		private static final int CAMERA_HEIGHT = 320;

        // ===========================================================
        // Fields
        // ===========================================================

        private Camera mCamera;
        
        private BitmapTextureAtlas mFontTexture;
        private BitmapTextureAtlas mTitleFontTexture;
        private BitmapTextureAtlas mBackTexture;
        
        private TextureRegion mBack;
        
        private Font mFont;
        private Font mTitleFont;
        
        private Sprite backButton;

        // ===========================================================
        // Constructors
        // ===========================================================

        // ===========================================================
        // Getter & Setter
        // ===========================================================

        // ===========================================================
        // Methods for/from SuperClass/Interfaces
        // ===========================================================

        @Override
        public Engine onLoadEngine() {
                this.mCamera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
                return new Engine(new EngineOptions(true, ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT), this.mCamera));
        }

        @Override
        public void onLoadResources() {
        		BitmapTextureAtlasTextureRegionFactory.setAssetBasePath("gfx/");
        	
                this.mFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                this.mTitleFontTexture = new BitmapTextureAtlas(256, 256, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
                this.mBackTexture = new BitmapTextureAtlas(1024, 1024, TextureOptions.BILINEAR);
                
                FontFactory.setAssetBasePath("font/");
        		this.mFont = FontFactory.createFromAsset(this.mFontTexture, this, "courbd.ttf", 16, true, Color.BLACK);
        		this.mTitleFont = FontFactory.createFromAsset(this.mTitleFontTexture, this, "Flubber.ttf", 32, true, Color.BLACK);
        		
        		this.mEngine.getTextureManager().loadTexture(this.mFontTexture);
        		this.mEngine.getTextureManager().loadTexture(this.mTitleFontTexture);
        		
        		this.mBack=BitmapTextureAtlasTextureRegionFactory.createFromAsset(this.mBackTexture, this, "back.png", 0, 0);
        		
        		this.mEngine.getTextureManager().loadTexture(this.mBackTexture);
        		
        		this.mEngine.getFontManager().loadFont(this.mFont);
        		this.mEngine.getFontManager().loadFont(this.mTitleFont);
        }

        @Override
        public Scene onLoadScene() {
                this.mEngine.registerUpdateHandler(new FPSLogger());

                final Scene scene = new Scene();
                scene.setBackground(new ColorBackground(0.0f, 0.8588f, 0.356f));
                                
                DbHelper dbhelper = new DbHelper(this);
                
                int scoreArray[] = dbhelper.getHighScores();
                
                final Text textTitle = new Text(CAMERA_WIDTH/2 - 100, CAMERA_HEIGHT/10, this.mTitleFont, "High Scores!", HorizontalAlign.CENTER);
                final Text text1 = new Text(CAMERA_WIDTH/10, (6*CAMERA_HEIGHT/20), this.mFont, "#1 : " + scoreArray[0], HorizontalAlign.LEFT);
                final Text text2 = new Text(CAMERA_WIDTH/10, (9*CAMERA_HEIGHT/20), this.mFont, "#2 : " + scoreArray[1], HorizontalAlign.LEFT);
                final Text text3 = new Text(CAMERA_WIDTH/10, (12*CAMERA_HEIGHT/20),this.mFont, "#3 : " + scoreArray[2], HorizontalAlign.LEFT);
                final Text text4 = new Text(CAMERA_WIDTH/10, (15*CAMERA_HEIGHT/20),this.mFont, "#4 : " + scoreArray[3], HorizontalAlign.LEFT);
                final Text text5 = new Text(CAMERA_WIDTH/10, (18*CAMERA_HEIGHT/20), this.mFont, "#5 : " + scoreArray[4], HorizontalAlign.LEFT);
                final Text text6 = new Text((7*CAMERA_WIDTH/10), (6*CAMERA_HEIGHT/20), this.mFont, "#6 : " + scoreArray[5], HorizontalAlign.LEFT);
                final Text text7 = new Text((7*CAMERA_WIDTH/10), (9*CAMERA_HEIGHT/20), this.mFont, "#7 : " + scoreArray[6], HorizontalAlign.LEFT);
                final Text text8 = new Text((7*CAMERA_WIDTH/10), (12*CAMERA_HEIGHT/20), this.mFont, "#8 : " + scoreArray[7], HorizontalAlign.LEFT);
                final Text text9 = new Text((7*CAMERA_WIDTH/10), (15*CAMERA_HEIGHT/20), this.mFont, "#9 : " + scoreArray[8], HorizontalAlign.LEFT);
                final Text text10 = new Text((7*CAMERA_WIDTH/10), (18*CAMERA_HEIGHT/20), this.mFont, "#10 : " + scoreArray[9], HorizontalAlign.LEFT);
                

                scene.attachChild(textTitle);
                scene.attachChild(text1);
                scene.attachChild(text2);
                scene.attachChild(text3);
                scene.attachChild(text4);
                scene.attachChild(text5);
                scene.attachChild(text6);
                scene.attachChild(text7);
                scene.attachChild(text8);
                scene.attachChild(text9);
                scene.attachChild(text10);
                
                backButton = new Sprite(15, 15, this.mBack){
        			@Override
        			public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX,
        					final float pTouchAreaLocalY){
        				switch(pSceneTouchEvent.getAction()){
        					case TouchEvent.ACTION_DOWN:
        						break;
        					case TouchEvent.ACTION_UP:
        						// will later bring up the pause menu
        						//Toast.makeText(AutoParallaxBackgroundExample.this, "Pause button pressed!", Toast.LENGTH_SHORT).show();
        						ScoresActivity.this.finish();
        						Intent myIntent = new Intent(ScoresActivity.this,MainMenuActivity.class);
        						ScoresActivity.this.startActivity(myIntent);
        						break;
        					default:
        						break;
        				}
        				return true;
        			}
        		};
        		scene.registerTouchArea(backButton);
                
        		scene.attachChild(backButton);

                return scene;
        }

        @Override
        public void onLoadComplete() 
        {

        }

		@Override
		public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
				ITouchArea pTouchArea, float pTouchAreaLocalX,
				float pTouchAreaLocalY) {
			// TODO Auto-generated method stub
			return false;
		}

        // ===========================================================
        // Methods
        // ===========================================================

        // ===========================================================
        // Inner and Anonymous Classes
        // ===========================================================
}